package edu.ccut.saturn.studio.validate.part;

import java.util.Map;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

import edu.ccut.saturn.studio.saturndata.model.ValidateModel;

//import edu.ccut.saturn.studio.validate.model.ValidateModel;

/**
 * 表格单元修改器
 * 
 * @author Bai Yucheng
 * 
 */
public class TableViewerCellModifierForErrValidate implements ICellModifier {
	private static final String TABLE_PROPERTY_VALUE = "value";
	private TableViewer tv;
	private ValidateModel dm;
	private Map<String, String> args;

	public TableViewerCellModifierForErrValidate(TableViewer tv,
			ValidateModel dm, Map<String, String> argMap) {
		this.tv = tv;
		this.dm = dm;
		this.args = argMap;
	}

	/**
	 * 返回是否可以被修改，true为可修改
	 */
	public boolean canModify(Object element, String property) {
		return true;
	}

	/**
	 * 返回列的值：参数element是表格记录的对象，property是表格列的属性
	 */
	public Object getValue(Object element, String property) {
		String argsStr = (String) element;
		if (property.equals(TABLE_PROPERTY_VALUE)) {
			return String.valueOf(argsStr.split("::")[1]);
		} else {
			return null;
		}
	}

	/**
	 * 修改表格单元中的值
	 * 
	 * @param element
	 *            表格记录的对象
	 * @param property
	 *            表格列的属性
	 * @param value
	 *            修改的新值
	 */
	public void modify(Object element, String property, Object value) {
		TableItem tableItem = (TableItem) element;
		String oldValue = (String) tableItem.getData();

		if (property.equals(TABLE_PROPERTY_VALUE)) {
			String newValue = (String) value;
			if (newValue.equals("")) {
				return;

			} else {
				String[] error = oldValue.split("::");

				oldValue = error[0] + "::" + newValue;
				args.put(error[0], newValue);
				
				}
			}
			dm.updateErrorinfo(this.args);
			tv.refresh();
		}
	}
